# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_pyname=babel
_realname=babel
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.7.0
_core=35
pkgrel=2
pkgdesc="A collection of tools for internationalizing Python applications (mingw-w64)"
url="http://babel.pocoo.org/"
license=("BSD")
arch=('any')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-pytz"
             "${MINGW_PACKAGE_PREFIX}-python3-pytz")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-pytest"
              "${MINGW_PACKAGE_PREFIX}-python3-pytest"
              "${MINGW_PACKAGE_PREFIX}-python2-pytest-runner"
              "${MINGW_PACKAGE_PREFIX}-python3-pytest-runner"
              "${MINGW_PACKAGE_PREFIX}-python2-freezegun"
              "${MINGW_PACKAGE_PREFIX}-python3-freezegun")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/python-babel/babel/archive/v${pkgver}.tar.gz"
           "core-$_core.zip"::"https://unicode.org/Public/cldr/$_core/core.zip")
noextract=("core-$_core.zip")
sha256sums=('d31a563d7b573f2544d49fecc22b58d470a624ebb63fe41583502095e266c462'
            '2c2fdaaf048d6ecdad7fa535be7a6bf232f1f02c9215edc31a26f1ede836ac48')

prepare() {
  cd ${srcdir}
  for pver in {2,3}; do 
    rm -rf python${pver}-build-${CARCH} | true
    cp -r "${_pyname}-${pkgver}" "python${pver}-build-${CARCH}"
    cp "$srcdir"/core-$_core.zip "python${pver}-build-${CARCH}"/cldr/core-$_core.0.zip
  done
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python${pver} setup.py import_cldr
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

check() {
  cd "${srcdir}"
  for pver in {2,3}; do  
    msg "Python ${pver} test for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
      ${MINGW_PREFIX}/bin/python${pver} setup.py pytest || true
  done  
}

package_python3-babel() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-pytz")

  cd "${srcdir}/python${pver}-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}

package_python2-babel() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-pytz")

  cd "${srcdir}/python${pver}-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done

  for f in pybabel; do
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe
    if [ -f "${pkgdir}${MINGW_PREFIX}"/bin/${f}.exe.manifest ]; then
      mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}.exe.manifest
      sed -e "s|${f}|${f}2|g" -i "${pkgdir}${MINGW_PREFIX}"/bin/${f}2.exe.manifest
    fi
    mv "${pkgdir}${MINGW_PREFIX}"/bin/${f}{,2}-script.py
  done
}

package_mingw-w64-i686-python2-babel() {
  install=${_realname}2-${CARCH}.install
  package_python2-babel
}

package_mingw-w64-i686-python3-babel() {
  install=${_realname}3-${CARCH}.install
  package_python3-babel
}

package_mingw-w64-x86_64-python2-babel() {
  install=${_realname}2-${CARCH}.install
  package_python2-babel
}

package_mingw-w64-x86_64-python3-babel() {
  install=${_realname}3-${CARCH}.install
  package_python3-babel
}
